package com.sskj.bibi.http;


import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.sskj.bibi.bean.CoinTypeBean;
import com.sskj.bibi.bean.CoinFee;
import com.sskj.bibi.bean.EntrustBean;
import com.sskj.bibi.bean.UserCoinInfo;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.lib.Constans;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.SocketSendBean;
import com.sskj.lib.bean.StockCoinBean;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.util.CommonUtil;

import java.util.HashMap;
import java.util.List;

import okhttp3.WebSocket;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-15 14:08
 */
public class HttpService {


    public PostRequest<HttpData<PageBean<EntrustBean>>> getCoinEntrustingList(String code) {
        return OkGo.<HttpData<PageBean<EntrustBean>>>post(HttpConfig.BASE_URL + HttpConfig.COIN_ENTRUSTING_LIST)
                .params("stockCode", code);
    }


    public PostRequest<HttpData> cancelCoinOrder(String orderId) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.COIN_ORDER_CANCEL)
                .params("order_id", orderId);
    }

    public WebSocketObserver pushCoinFive(String code) {
        return new WebSocketObserver(HttpConfig.WS_BASE_URL) {

            public void onOpen(WebSocket webSocket, okhttp3.Response response, String msg) {
                webSocket.send("{\"sub\":\"depth."+code+"\"}");
            }
        };
    }

    public GetRequest<HttpData<List<CoinBean>>> getProduct(String qu, String code) {


        return OkGo.<HttpData<List<CoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.PRODUCT_LIST)
//        return OkGo.<HttpData<List<CoinBean>>>get("http://113.52.135.113" + HttpConfig.PRODUCT_LIST)
                .params("qu", qu)
                .params("code", CommonUtil.dealReuqestCode(code))
                ;
    }

    public GetRequest<HttpData<List<WSFiveBean>>> getDeepData(String code) {
        return OkGo.<HttpData<List<WSFiveBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_DEEP)
                .params("code", code)
                ;
    }


    public GetRequest<HttpData<List<CoinTypeBean>>> getAllCoinInfo(String qu) {
        return OkGo.<HttpData<List<CoinTypeBean>>>get(HttpConfig.BASE_URL + HttpConfig.CURRENCYLIST)
                .params("type1", qu.equals(Constans.DANWEI) ? "2" : "1")
                .params("type", "0")
                ;

    }


    public PostRequest<HttpData<PageBean<EntrustBean>>> getBibiTradeRecord(String code, String type, String status, int page, String qu) {
//        if (status.equals("2,3")) {
//            return getCoinEntrustListAll(code, type, status);
//        } else {
//            return getCoinEntrustList(code, type, status);
//        }
        return getCoinEntrustList(code, type, status, page, qu);


    }


    public PostRequest<HttpData<List<EntrustBean>>> getBibiCurrentTrade(String code, String type) {
        return OkGo.<HttpData<List<EntrustBean>>>post(HttpConfig.BASE_URL + HttpConfig.COIN_ENTRUST_LIST_ALL)
                .params("stockCode", code)
                .params("tradeType", type)
                .params("stateStr", "0,1");
    }


    public PostRequest<HttpData<PageBean<EntrustBean>>> getCoinEntrustList(String code, String type, String state, int page, String qu) {
        return OkGo.<HttpData<PageBean<EntrustBean>>>post(HttpConfig.BASE_URL + HttpConfig.COIN_ENTRUST_LIST)
                .params("code", CommonUtil.dealReuqestCode(code))
                .params("type", type)
                .params("type1", qu.equals(Constans.DANWEI) ? "2" : "1")
                .params("p", page)
                .params("size", 10)
                .params("status", state);
    }


    public PostRequest<HttpData<PageBean<EntrustBean>>> getCoinEntrustListAll(String code, String type, String state) {
        return OkGo.<HttpData<PageBean<EntrustBean>>>post(HttpConfig.BASE_URL + HttpConfig.COIN_ENTRUST_LIST_ALL)
                .params("stockCode", code)
                .params("tradeType", type)
                .params("stateStr", state);
    }


    public PostRequest<HttpData<UserCoinInfo>> getCoinTypeInfo(String code) {
        return OkGo.<HttpData<UserCoinInfo>>post(HttpConfig.BASE_URL + HttpConfig.ONE_COIN_INFO)
                .params("stockCode", code);
    }


    public PostRequest<HttpData<CoinFee>> getCoinFee(String code) {
        return OkGo.<HttpData<CoinFee>>post(HttpConfig.BASE_URL + HttpConfig.COIN_FEE)
                .params("code", CommonUtil.dealReuqestCode(code))
                .params("mark", CommonUtil.dealReuqestCode(code))
                ;
    }


    public PostRequest<HttpData> createCoinOrder(String code, String price, String num, String tradeType, String billPriceType) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CREATE_COIN_ORDER)
                .params("code", CommonUtil.dealReuqestCode(code))
                .params("type", tradeType)
                .params("otype", billPriceType)
                .params("buynum", num)
                .params("toalprice", num)
                .params("toalnum", num)
                .params("buyprice", price);
    }


    public GetRequest<HttpData<List<WSFiveBean>>> getPankou(String code) {
        return OkGo.<HttpData<List<WSFiveBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_PANKOU)
                .params("code", CommonUtil.dealReuqestCode(code));

    }


    public WebSocketObserver pushCoinFiveDeep(String code) {
        return new WebSocketObserver(HttpConfig.WS_BASE_URL) {

            public void onOpen(WebSocket webSocket, okhttp3.Response response, String msg) {
                webSocket.send("{\"sub\":\"pct."+code+"\"}");
            }
        };
    }
}